package com.android.jmdnstest;

import android.app.Activity;
import android.content.Context;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.util.Log;

import java.io.IOException;

public class MulticastDemoActivity extends Activity {

    WifiManager.MulticastLock multicastLock;

    /**
     * Called when the activity is first created.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_multicast_demo);

        allowMulticast();

        new Thread(new Runnable() {
            @Override
            public void run() {
                String serverIpAddress = null;
                try {
                    serverIpAddress = NetUtil.findServerIpAddress();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                Log.e("serverIpAddress", serverIpAddress);
            }
        }).start();


        Log.d("multicast.demo", "find ip ok.");

        multicastLock.release();
    }

    private void allowMulticast() {
        WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
        multicastLock = wifiManager.createMulticastLock("multicast.test");
        multicastLock.acquire();
    }
}
